#=============================================================================
# Manuscript: Pushing boundaries: How lawmakers shape judicial decision-making 
#=============================================================================

# Journal: Comparative Political Studies
# Author: Philipp Schroeder (LMU Munich)
# For questions contact P.Schroeder@gsi.uni-muenchen.de
# Date: 2021-10-08
# R version 4.0.4 (2021-02-15) -- "Lost Library Book"

### Data sourcing ###

rm(list=ls())
library(arm)

getwd()
mydata <- read.csv("data_cleaned1977-2017.csv", sep = ";", encoding="UTF-8")
mp_information <- read.csv("mp_information.csv", sep = ";", encoding="UTF-8")

names(mydata)
select <- c("obj_gov", "obj_opp","auth_gov","speech_gov","written_gov","speech_opp","written_opp","written_cdu", "written_spd", "written_fdp", "written_green", "written_left",
            "obj_cdu", "obj_spd", "obj_fdp", "obj_green", "obj_left", "obj_ind")
mydata$obj_paragraph[is.na(mydata$obj_paragraph)] <- 0
mydata[mydata$obj_paragraph == 0,select] <- 0
table(mydata$obj_paragraph)
names(mydata)
length(unique(mydata$law_id)) # number of laws in dataset

# law and case date
mydata$law_date <- as.Date(mydata$law_date, "%d.%m.%Y")
mydata$case_date <- as.Date(mydata$case_date, "%d.%m.%Y")

# get time between adoption and decision
mydata$days_passed <- mydata$case_date - mydata$law_date
summary(mydata$days_passed)
mydata$days_passed[1:10]
mydata$z.days_passed <- rescale(mydata$days_passed)

# identify senate
mydata$case <- as.character(mydata$case)
mydata$senate <- as.factor(substr(mydata$case, 1, 1))
summary(mydata$senate)

# distribution of outcome
table(mydata$court_decision)
mean(mydata$court_decision)

# identify parliament term
mydata$term <- substr(mydata$law_id, 1, 2)
mydata$term[mydata$term == "8-"] <- "8"
mydata$term[mydata$term == "9-"] <- "9"
table(mydata$term)

#================================================================
# B) Descriptive statistics

# sum objections
mydata$cdu <- mydata$obj_cdu + mydata$written_cdu
mydata$spd <- mydata$obj_spd + mydata$written_spd
mydata$fdp <- mydata$obj_fdp + mydata$written_fdp
mydata$green <- mydata$obj_green + mydata$written_green
mydata$left <- mydata$obj_left + mydata$written_left
mydata$ind <- mydata$obj_ind + mydata$written_ind

# Objections in government (by party)
cdu_gov <- summary(mydata$cdu[mydata$law_date >= as.Date("1982-10-01") | mydata$law_date < as.Date("1983-03-29") | mydata$term %in% c("10","11","12","13","16","17","18")])
spd_gov <- summary(mydata$spd[mydata$law_date < as.Date("1982-10-01") | mydata$term %in% c("14","15","16","18")])
fdp_gov <- summary(mydata$fdp[mydata$term %in% c("8","9","10","11","12","13","17")])
green_gov <- summary(mydata$green[mydata$term %in% c("14","15")])
# counts
count_cdu_gov <- sum(mydata$cdu[mydata$term %in% c("9","10","11","12","13","16","17","18")])
count_spd_gov <- sum(mydata$spd[mydata$term %in% c("8","14","15","16","18")])
count_fdp_gov <- sum(mydata$fdp[mydata$term %in% c("8","9","10","11","12","13","17")])
count_green_gov <- sum(mydata$green[mydata$term %in% c("14","15")])

# Objections in opposition (by party)
cdu_opp <- summary(mydata$cdu[mydata$law_date < as.Date("1982-10-01") | mydata$term %in% c("14","15")])
spd_opp <- summary(mydata$spd[mydata$law_date >= as.Date("1982-10-01") | mydata$law_date < as.Date("1983-03-29") | mydata$term %in% c("10","11","12","13","17")])
fdp_opp <- summary(mydata$fdp[mydata$term %in% c("14","15","16","18")])
green_opp <- summary(mydata$green[!mydata$term %in% c("14","15")])
left_opp <- summary(mydata$left)
# counts
count_cdu_opp <- sum(mydata$cdu[mydata$term %in% c("8","14","15")])
count_spd_opp <- sum(mydata$spd[mydata$term %in% c("9","10","11","12","13","17")])
count_fdp_opp <- sum(mydata$fdp[mydata$term %in% c("14","15","16","18")])
count_green_opp <- sum(mydata$green[!mydata$term %in% c("14","15")])
count_left_opp <- sum(mydata$left)

# per provision
table(mydata$obj_opp)
table(mydata$obj_gov)

mydata$count_gov <- mydata$speech_gov + mydata$written_gov
mydata$count_opp <- mydata$speech_opp + mydata$written_opp

# government objections
summary(mydata$count_gov)
summary(mydata$speech_gov)
summary(mydata$written_gov)
sd(mydata$speech_gov)
sd(mydata$written_gov)

# opposition objections
summary(mydata$count_opp)
summary(mydata$speech_opp)
summary(mydata$written_opp)
sd(mydata$speech_opp)
sd(mydata$written_opp)

# standardize
mydata$z.count_gov <- rescale(mydata$count_gov)
mydata$z.count_opp <- rescale(mydata$count_opp)

mydata$z.speech_gov <- rescale(mydata$speech_gov)
mydata$z.speech_opp <- rescale(mydata$speech_opp)
mydata$z.written_gov <- rescale(mydata$written_gov)
mydata$z.written_opp <- rescale(mydata$written_opp)

# dummies for speech and declarations
mydata$speech_gov_dummy <- ifelse(mydata$speech_gov > 0, 1, 0) 
mydata$written_gov_dummy <- ifelse(mydata$written_gov > 0, 1, 0) 
mydata$speech_opp_dummy <- ifelse(mydata$speech_opp > 0, 1, 0) 
mydata$written_opp_dummy <- ifelse(mydata$written_opp > 0, 1, 0) 
table(mydata$speech_gov_dummy)
table(mydata$written_gov_dummy)
table(mydata$speech_opp_dummy)
table(mydata$written_opp_dummy)

# Get distribution of senior governing lawmakers objecting
table(mydata$senior_gov)
mydata$senior_gov_cat[mydata$senior_gov == 1] <- "senior governing lawmaker"
mydata$senior_gov_cat[mydata$senior_gov == 0] <- "junior governing lawmaker"
mydata$senior_gov_cat[mydata$obj_gov == 0] <- "no governing lawmaker"
mydata$senior_gov_cat <- factor(mydata$senior_gov_cat, levels = c("no governing lawmaker","junior governing lawmaker","senior governing lawmaker"))
table(mydata$senior_gov_cat)

# Get time in office for objecting governing lawmaker
mp_information$entry <- as.Date(mp_information$entry, "%d.%m.%Y")
mp_information$law_date <- as.Date(mp_information$law_date, "%d.%m.%Y")

mp_information$time_as_mp <- mp_information$law_date - mp_information$entry

mp_information$time_as_mp[1:10]
mp_information$entry[1:10]
mp_information$law_date[1:10]
mean(mp_information$time_as_mp)

# Get most senior objecting governing lawmaker
str(mydata$senior_entry)
mydata$senior_entry[1:100]
mydata$senior_entry <- as.Date(mydata$senior_entry, "%Y.%m.%d")
mydata$obj_gov_time_as_mp <- mydata$law_date - mydata$senior_entry
mydata$obj_gov_time_as_mp[is.na(mydata$obj_gov_time_as_mp)] <- 0
mydata$z.obj_gov_time_as_mp <- rescale(mydata$obj_gov_time_as_mp)

#================================================================
# C) Plots

# Descriptive statistics for objections
pdf("descriptive.pdf", height = 5, width = 8) # Figure 4
par(mfrow = c(1,2), mar = c(5, 4, 2, 4), oma = c(0, 0, 2, 0))
opp <- barplot(table(mydata$obj_opp), main="",
               xlab="Constitutionality contested by opposition lawmakers",
               ylim = c(0, 400))
text(opp, 0, table(mydata$obj_opp),cex=0.7,pos=3)
gov <- barplot(table(mydata$obj_gov), main="",
               xlab="Constitutionality contested by governing lawmakers",
               ylim = c(0, 400))
text(gov, 0, table(mydata$obj_gov),cex=0.7,pos=3)
mtext("Contested legislative provisions", outer = TRUE)
dev.off()

# Party affiliation of lawmakers objecting
pdf("lawmakers.pdf", height = 5, width = 10) # Figure 1 Appendix
par(mfrow = c(1,2), mar = c(5, 4, 2, 4), oma = c(0, 0, 2, 0))
opp <- barplot(c(count_cdu_opp,count_spd_opp,count_fdp_opp,count_green_opp,count_left_opp),
               names = c("CDU/CSU","SPD","FDP","Greens","Linke"),
               cex.names = 0.9,
               main = "Opposition lawmakers",
               ylim = c(0,900))
gov <- barplot(c(count_cdu_gov,count_spd_gov,count_fdp_gov,count_green_gov),
        names = c("CDU/CSU","SPD","FDP","Greens"),
        cex.names = 0.9,
        main = "Governing lawmakers",
        ylim = c(0,250))
dev.off()

# Distribution of objection counts
pdf("counts.pdf", height = 7, width = 10) # Figure 2 Appendix
par(mfrow = c(2,1))
hist(mydata$count_opp[mydata$count_opp > 0], breaks = 100, xlim = c(1,120), xaxt='n',
     xlab = "Count of objections by opposition lawmakers",
     main = "Counts of lawmakers voicing constitutional concerns")
axis(side=1, at=(c(1,seq(10,120,10))))
hist(mydata$count_gov[mydata$count_gov > 0], breaks = 100, xlim = c(1,120), xaxt='n',
     xlab = "Count of objections by governing lawmakers",
     main = "")
axis(side=1, at=(c(1,seq(10,120,10))))
dev.off()

pdf("seniority.pdf", height = 4, width = 10) # Figure 3 Appendix
hist(as.numeric(mp_information$time_as_mp),
     xlab = "Time as Member of Parliament",
     main = "Seniority of governing lawmakers voicing constitutional concerns")
dev.off()

# Distribution of control variables
pdf("controls.pdf", height = 7, width = 10) # Figure 4 Appendix
par(mfrow = c(1,2))
barplot(table(mydata$cross_party),
        main = "Cross-party proposals")
par(mar = c(5,11,2,4))
barplot(height = table(mydata$policy_area),
        horiz = TRUE,
        cex.names = 0.8, las = 1,
        xlim = c(0,100),
        main = "Policy areas")
dev.off()
